﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace parkingclient
{
    public partial class User_Registration_Page : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";

            if (Session["user"] == null)
            {
                lblWelcome.Text = "[<a href='user_login.aspx'>Login</a>]   [<a href='registration.aspx'>Register</a>]";
            }
            else if (int.Parse(Session["userType"].ToString()) == 3) //for admin, add link to the admin dashboard
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='admin_dashboard.aspx'>Admin Dashboard</a>]    [<a href='user_logout.aspx'>Logout</a>]";
            }
            else
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='user_logout.aspx'>Logout</a>]";
            }
        }


        protected void btnRegister_Click(object sender, EventArgs e)
        {
            //blank field check
            string err = "You must specify ";
            if (txtEmail.Text == "") err += "email, ";
            if (txtPassword1.Text == "") err += "password, ";
            if (txtFirstName.Text == "") err += "first name, ";
            if (txtLastName.Text == "") err += "last name, ";
            if (txtAddress.Text == "") err += "address, ";
            if (txtCity.Text == "") err += "city, ";
            if (txtZip.Text == "") err += "zip code, ";

            if (err != "You must specify ")
            {
                err = err.Substring(0, err.Length - 2);
                lblError.Text = err;
                return;
            }



            //password check
            if (txtPassword1.Text != txtPassword2.Text)
            {
                lblError.Text = "Password must match!";
                return;
            }

            //zip code check
            int num;
            if (!Int32.TryParse(txtZip.Text, out num) || txtZip.Text.Length != 5)
            {
                lblError.Text = "Zip code must be in a 5-digit format";
                return;
            }


            //create the account
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            int accountNum = parking.createAccount(txtEmail.Text, txtPassword1.Text, Int32.Parse(txtMembershipType.Text), txtFirstName.Text, txtLastName.Text, txtAddress.Text, txtCity.Text, txtState.Text, txtZip.Text);

            if (accountNum == -1)
            {
                lblError.Text = "Unexpected error is detected. Please check your entry";
                return;
            }
            else
            {
                //if student, go to payment page, otherwise account creation is done
                if (Int32.Parse(txtMembershipType.Text) == 1)
                {
                    Response.Redirect("Payment_Details.aspx");
                }
                else
                {
                    Response.Write("<script>alert('Your account has been successfully created. You will be redirected to the Welcome page now'); self.location='Default.aspx';</script>");
                }
            }


        }
    }
}